<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keyword" placeholder="关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="seach">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="employees" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="username" label="管理员" width="120" sortable>
      </el-table-column>
      <el-table-column prop="email" label="邮箱" width="180" sortable>
      </el-table-column>
      <el-table-column prop="phone" label="联系电话" width="160" sortable>
      </el-table-column>
      <el-table-column prop="age" label="年龄" width="180" sortable>
      </el-table-column>

      <el-table-column prop="state" label="状态" width="280" sortable>
        <template scope="scope">
          <span style="color: #5daf34" v-if="scope.row.state === 1">启用</span>
          <span style="color:red" v-else-if="scope.row.state === 0">禁用</span>
          <span style="color:dimgrey" v-else>未知</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" >
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next"
                     @current-change="handleCurrentChange"
                     :page-size="pageSize"
                     :total="total"
                     style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog :title="yoyo" :visible.sync="employeeFormVisible" :close-on-click-modal="false">
      <el-form :model="employeeForm" label-width="80px" :rules="employeeFormRules" ref="employeeForm">
        <el-form-item label=" 管理员" prop="username">
          <el-input v-model="employeeForm.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="部门邮箱" prop="email">
          <el-input v-model="employeeForm.email" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="employeeForm.phone" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="employeeForm.age" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="employeeForm.password" auto-complete="off"></el-input>
        </el-form-item>


        <el-form-item label="状态">
          <el-radio-group v-model="employeeForm.state">
            <el-radio class="radio" :label="1">开启</el-radio>
            <el-radio class="radio" :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="employeeFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="employeeSubmit" :loading="employeeLoading">提交</el-button>
      </div>
    </el-dialog>



  </section>
</template>

<script>
import util from '../../common/js/util'
//import NProgress from 'nprogress'
import { getEmployeeListPage, removeEmployee, batchRemoveEmployee, employeeEmployee, addEmployee } from '../../api/api';

export default {
  data() {
    return {
      filters: {
        keyword: ''
      },
      employees:[],

      yoyo:"",
      total: 0,
      pageSize:5,
      currentPage: 1,
      listLoading: false,
      sels: [],//列表选中行

      employeeFormVisible: false,//编辑界面是否显示
      employeeLoading: false,
      employeeFormRules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入电话', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      },


      //编辑界面数据
      employeeForm: {
        id: null,
        email: "",
        password:"",
        username: "",
        state: 0,
        manager: null,
        parent: null,
        phone:""
      },
    }
  },


  methods: {
    handleCurrentChange(val) {
      this.currentPage= val;

      this.getEmployee();
    },

    seach(){
      this.currentPage = 1;
      this.getEmployee();
    },

    //获取部门列表
    getEmployee() {
      let para = {
        currentPage: this.currentPage,
        keyword: this.filters.keyword,
        pageSize:this.pageSize
      };
      this.listLoading = true;//开启忙等框
      this.$http.post("/employee",para)
          .then(result=>{
            result = result.data;//PageList{total rows}
            this.employees = result.rows;
            this.listLoading = false;//关闭忙等框
            this.total = result.total;

          })
          .catch(result=>{
            console.log(result);
            alert("系统错误！");
          })

    },



    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '温馨提示', {
        type: 'warning'
      }).then(() => {//点击确定
        this.listLoading = true;//开启忙等框
        //NProgress.start();
        this.$http.delete("/employee/"+row.id)
            .then(result=>{
              //1.关闭忙等
              this.listLoading = false;
              //判断业务操作是否正常
              result  = result.data;//AjaxResult  {success:false,message:操作失败}
              if(result.success){
                this.$message({
                  message: '删除成功',
                  type: 'success'
                });
              }else{
                this.$message({
                  message: result.message,//提示我们自己的业务错
                  type: 'error'
                });
              }
              //2.刷新页面
              //是否需要重新刷新页面   要  定位到第一页
              this.currentPage= 1;
              this.getEmployee();
            })
            .catch(result=>{
              this.listLoading = false;
              //NProgress.done();
              this.$message({
                message: '删除失败',
                type: 'error'
              });
              //失败了是否需要重新刷新页面   要  定位到第一页
              this.currentPage= 1;
              this.getEmployee();
            })
      }).catch(() => {//点击取消

      });
    },



    //显示编辑界面
    handleEdit: function (index, row) {
       
      this.yoyo="编辑";
      this.employeeFormVisible = true;
      //Object.assign 克隆
      this.employeeForm = Object.assign({}, row);//回显
     
    },
    //显示新增界面
    handleAdd: function () {
       
      this.yoyo="新增";
      this.employeeFormVisible = true;
      this.employeeForm = {//清空历史遗留数据
        id: null,
        email: "",
        username: "",
        password:"",
        state: 0,
        manager: null,
        parent: null,
        phone:""
      };
    },




    //编辑
    employeeSubmit: function () {
      this.$refs.employeeForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.employeeLoading = true;
            //NProgress.start();
            let para = Object.assign({}, this.employeeForm);
           //下午要用
            if(para.parent){
              para.parent={"id":parseInt(para.parent[para.parent.length-1])};
            }
            this.$http.put("/employee",para)
                .then(result=>{
                  //1.关闭忙等
                  this.listLoading = false;
                  //判断业务操作是否正常
                  result  = result.data;//AjaxResult  {success:false,message:操作失败}
                  if(result.success){
                    this.employeeLoading = false;
                    this.employeeFormVisible = false;
                    this.$message({
                      message: '操作成功',
                      type: 'success'
                    });
                  }else{
                    this.employeeLoading = false;
                    this.$message({
                      message: result.message,//提示我们自己的业务错
                      type: 'error'
                    });
                  }
                  //2.刷新页面
                  //是否需要重新刷新页面   要  定位到第一页
                  this.currentPage= 1;
                  this.filters.keyword="";
                  this.getEmployee();
                })
                 .catch(result=>{
                   this.employeeLoading = false;
                  //NProgress.done();
                  this.$message({
                    message: '操作失败',
                     type: 'error'
                  });
                  //失败了是否需要重新刷新页面   要  定位到第一页
                   this.currentPage= 1;
                   this.filters.keyword="";
                   this.getEmployee();
                 })
          });
        }
      });
    },
    selsChange: function (sels) {//当前已经选中的所有行的数据
      this.sels = sels;
    },



    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);//[1,2,3]
      this.$confirm('确认删除选中记录吗？', '温馨提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        this.$http.patch("/employee",ids)
            .then(result=>{
              //1.关闭忙等
              this.listLoading = false;
              //判断业务操作是否正常
              result  = result.data;//AjaxResult  {success:false,message:操作失败}
              if(result.success){
                this.$message({
                  message: '删除成功',
                  type: 'success'
                });
              }else{
                this.$message({
                  message: result.message,//提示我们自己的业务错
                  type: 'error'
                });
              }
              //2.刷新页面
              //是否需要重新刷新页面   要  定位到第一页
              this.currentPage= 1;
              this.getEmployee();
            })
            .catch(result=>{
              this.listLoading = false;
              //NProgress.done();
              this.$message({
                message: '删除失败',
                type: 'error'
              });
              //失败了是否需要重新刷新页面   要  定位到第一页
              this.currentPage= 1;
              this.getEmployee();
            })
      }).catch(() => {

      });
    },


  },


  mounted() {
    this.getEmployee();

  }
}

</script>

<style scoped>


</style>